package cn.yh.springbootjwt.controller;

import cn.yh.springbootjwt.common.AjaxResult;
import cn.yh.springbootjwt.common.TableDataInfo;
import cn.yh.springbootjwt.controller.base.BaseController;
import cn.yh.springbootjwt.domain.query.PageQuery;
import cn.yh.springbootjwt.domain.vo.ProductCategoryVo;
import cn.yh.springbootjwt.domain.vo.ProductDtlVo;
import cn.yh.springbootjwt.service.IProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(tags = "商品管理接口")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @ApiOperation(value = "分页查询商品信息",notes = "查询商品信息，需要实现分页查询、支持按分类筛选商品信息。")
    @GetMapping("/list")
    @ApiImplicitParam(name = "categoryId", value = "分类id", dataType = "Integer")
    public TableDataInfo<ProductCategoryVo> selectProductByCategoryId(@Validated PageQuery pageQuery , @RequestParam(required = false) Long categoryId){

        Page<ProductCategoryVo> page = PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        productService.selectProductByCategoryId(categoryId);

        return dataInfo(page.getResult(), page.getTotal());
    }

    @ApiOperation(value = "查询商品详情")
    @GetMapping("/find")
    @ApiModelProperty(value = "商品id", required = true, dataType = "Integer")
    @Validated
    public AjaxResult<ProductDtlVo> findByProductId(
            @NotNull(message = "商品ID不能为空")
            @Min(value = 1, message = "商品ID必须大于0")
            @RequestParam Long productId){
        System.out.println("productId: " + productId);
        return AjaxResult.success(productService.findByProductId(productId));
    }
}
